package com.wang.myblog.controller;

import com.alibaba.fastjson.JSONObject;
import com.wang.myblog.common.PageRequest;
import com.wang.myblog.common.PageResponse;
import com.wang.myblog.common.ResponseResult;
import com.wang.myblog.entity.Kind;
import com.wang.myblog.service.KindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class KindController {

    @Autowired
    private KindService kindService;

    @RequestMapping("/kindManage")
    public String kindManage(){
        return "admin/kinds";
    }
    /**
     * 获取分类管理页面需要的数据
     */

    @PostMapping("/kindInfo")
    @ResponseBody
    public PageResponse kindInfo(@RequestBody PageRequest request){
        request.setSearchMsg(request.getSearchMsg().replace(" ",""));
        if (request.getSearchMsg()=="")
            request.setSearchMsg(null);
        PageResponse pageResponse=kindService.queryByPageAndSearchMsg(request.getDraw(), request.getOffset(), request.getPageSize(), request.getSearchMsg());
        List<Kind> list=(List<Kind>) pageResponse.getData();
        return pageResponse;
    }

    @RequestMapping("/createKind")
    @ResponseBody
    public String createKind(@RequestBody Kind kind){
        System.out.println("add kind");
        ResponseResult result=new ResponseResult();
        if (kindService.queryByName(kind.getName())!=null) {
            result.setCode(500);
            result.setData(false);
            result.setMessage("类名已经存在");
        }else {
            Boolean b = kindService.insert(kind);
            result.setCode(200);

        }
        return JSONObject.toJSONString(result);
    }

    /**
     * 前往编辑分类界面
     */
    @RequestMapping("/editKind")
    public String editKind(Model model,Integer kindId){
        Kind kind=kindService.queryById(kindId);
        model.addAttribute("kind",kind);
        return "admin/editKind";
    }

    /**
     * 编辑分类
     */
    @RequestMapping("/doEditKind")
    @ResponseBody
    public String doEditKind(@RequestBody Kind kind){
        System.out.println("编辑分类");
        ResponseResult result=new ResponseResult();
        if(kindService.update(kind)){
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("错误");
        }
        return JSONObject.toJSONString(result);
    }

    /**
     * 删除分类，关联的文章分类置空
     */
    @RequestMapping("/deleteKind")
    @ResponseBody
    public String deleteKind(Integer kindId){
        ResponseResult result=new ResponseResult();
        if(kindService.deleteById(kindId)){
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("删除失败");
        }
        return JSONObject.toJSONString(result);
    }


}
